package com.wind.hallservice;

import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
//import android.app.StatusBarManager;

/*
 * wuguohu@wind-mobi.com create
 */
public class HallWidget extends FrameLayout implements View.OnClickListener{
	private static final String TAG = HallWidget.class.getSimpleName();
  private static final String KEYGUARD_HALL_CALL_SWITCH = "android.intent.action.KEYGUARD_HALL_CALL_SWITCH";
  private Context mContext;
	
	private HallClockView mClockView;
	private HallPhoneView mPhoneView;
	private ImageView mCallSwitch;
	
	private int mCallState;
	private String mIncomingNumber;
	private String mCallerName;
	
	private int mMissedCall = 0;
	private int mMissedMms = 0;
	private boolean isDualCalling = false;
	//private StatusBarManager mStatusBarManager;

	public HallWidget(Context context) {
		this(context, null, 0);
	}

	public HallWidget(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public HallWidget(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
		mContext = context;
	//	mStatusBarManager = (StatusBarManager) mContext.getSystemService(Context.STATUS_BAR_SERVICE);
	}

	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		Log.d(TAG, "onFinishInflate");
		
		mClockView = (HallClockView) findViewById(R.id.keyguard_page_clock);
		mPhoneView = (HallPhoneView) findViewById(R.id.keyguard_page_phone);
		mCallSwitch = (ImageView) findViewById(R.id.keyguard_call_switch);
		mCallSwitch.setOnClickListener(this);
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		Log.d(TAG, "onAttachedToWindow");
		
		//status bar
	//	mStatusBarManager.disable(StatusBarManager.DISABLE_EXPAND);
		//view
		checkViewVisible();
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		Log.d(TAG, "onDetachedFromWindow");
		
		//status bar
	//	mStatusBarManager.disable(StatusBarManager.DISABLE_NONE);
	}
	
	private void checkViewVisible() {
		Log.d(TAG, "checkViewVisible, mCallState: " + mCallState);
		if (mCallState == TelephonyManager.CALL_STATE_OFFHOOK
				|| mCallState == TelephonyManager.CALL_STATE_RINGING || mCallState == HallService.HALL_OUT_GOING_CALL) {
			mPhoneView.setVisibility(View.VISIBLE);
			mClockView.setVisibility(View.GONE);
			if(isDualCalling){
			  mCallSwitch.setVisibility(View.VISIBLE);
			}else{
	      mCallSwitch.setVisibility(View.GONE);
			}
		} else {
			mPhoneView.setVisibility(View.GONE);
			mClockView.setVisibility(View.VISIBLE);
			mCallSwitch.setVisibility(View.GONE);
		}
	}
	
	public void onCallStateChanged(int state, String incomingNumber){
		setCallState(state, incomingNumber);
		checkViewVisible();
	}
	
	public void onCallerNameReceived(String callerName){
		mCallerName = callerName;
		mPhoneView.onCallerNameReceived(mCallerName);
	}
	
	public void setCallState(int state, String incomingNumber){
		mCallState = state;
		mIncomingNumber = incomingNumber;
		
		if(state == TelephonyManager.CALL_STATE_IDLE){
			mIncomingNumber = "";
			mCallerName = "";
		}
		
		mPhoneView.setCallState(mCallState, mIncomingNumber);
	}
	
	public void setMissedInfo(int missedCall, int missedMms){
		mMissedCall = missedCall;
		mMissedMms = missedMms;
		
		mClockView.setMissedInfo(mMissedCall, mMissedMms);
	}

    //chenbin@wind-mobi.com start
	public void setHallStatus(int status)
	{
	   if(mPhoneView != null)
	   	{
	   	  mPhoneView.setHallstate(status);
	   	}
	}
	//chenbin@wind-mobi.com end
	public void setActivePhoneNumber(String num)
	{
	   if(mPhoneView != null)
	   	{
	   	  mPhoneView.setActivePhoneNumber(num);
	   	}
	}
	
	public void isDualCalling(boolean value){
		 isDualCalling = value;
		}
		
		@Override
	public void onClick(View v) {
		if (v == mCallSwitch){
		  makeCallSwitch();
		}
	}
	
	 public void makeCallSwitch(){
	 	Log.d(TAG, "makeCallSwitch");
	 	Intent intent = new Intent(KEYGUARD_HALL_CALL_SWITCH);
		mContext.sendBroadcast(intent);
	 	}
}
